package com.lj.demo.vodService.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lj.demo.commonutils.R;
import com.lj.demo.vodService.service.VideoAdminService;
import com.lj.demo.vodService.utils.AliyunVodSDKUtils;
import com.lj.demo.vodService.utils.ConstantPropertiesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: liujiang
 * @Date: 2020/12/13/22:45
 * @Description:
 */
@Api(value = "阿里云视频点播服务")
@RequestMapping("vodService/vodVideo")
@RestController
@CrossOrigin
public class VideoAdminController {

    @Autowired
    private VideoAdminService videoAdminService;


    @ApiOperation(value = "上传视频" )
    @PostMapping("uploadVideo")
    public R uploadVideo(@RequestParam("file") MultipartFile file) throws Exception{
        String videoId = videoAdminService.uploadVideo(file);
        return R.ok().message("视频上传成功").data("videoId",videoId);
    }


    @ApiOperation(value = "删除视频")
    @DeleteMapping("removeVideo/{videoId}")
    public R removeVideo(@PathVariable String videoId){
        videoAdminService.removeVideo(videoId);
        return R.ok().message("删除视频成功");
    }


    @ApiOperation(value = "批量删除视频")
    @DeleteMapping("removeVideoList")
    public R removeVideoList(@RequestParam("videoIdList")List<String> videoIdList){
        videoAdminService.removeVideoList(videoIdList);
        return R.ok().message("删除视频成功！");
    }

    @GetMapping("getVideoPlayAuth/{videoId}")
    public R getVideoPlayAuth(@PathVariable String videoId) throws Exception{
        //获取阿里云存储相关的常量
        //初始化
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        return R.ok().message("获取凭证成功").data("playAuth",playAuth);
    }

}
